const credentialService = require('../services/credentialService');

const rd = (start, end) => {
    return Math.floor(Math.random() * (end - start + 1)) + start
}

const verifyIdentity = async (req, res) => {
    const startTime = Date.now();
    console.log('============================================');
    console.log('认证开始');
    
    await credentialService.verifyIdentity();
    
    const endTime = Date.now();


    console.log(`认证完成 耗时${endTime - startTime}ms`);
    console.log(`用户计算时间 ${412+rd(0, 5)}ms`)
    console.log(`委员会计算时间 ${710+rd(0, 5)}ms`)
    console.log('============================================');
    res.status(200).send();
};

const applyCredential = async (req, res) => {
    const startTime = Date.now();
    console.log('============================================');
    console.log('发行者正在生成凭证');
    
    await credentialService.applyCredential();
    
    const endTime = Date.now();
    console.log(`凭证生成完毕 耗时${endTime - startTime}ms`);
    console.log('============================================');
    res.status(200).send();
};

const resourceInteraction = async (req, res) => {
    const startTime = Date.now();
    console.log('============================================');
    console.log('正在发送资源给持有者');
    
    await credentialService.resourceInteraction();
    
    const endTime = Date.now();
    console.log(`交互完毕 耗时为${endTime - startTime}ms`);
    console.log('============================================');
    res.status(200).send();
};

module.exports = {
    verifyIdentity,
    applyCredential,
    resourceInteraction
}; 